package com.rao.gmall.product.controller;

import com.rao.gmall.common.result.Result;
import com.rao.gmall.common.result.ResultCodeEnum;
import com.rao.gmall.model.product.BaseAttrInfo;
import com.rao.gmall.model.product.BaseAttrValue;
import com.rao.gmall.product.service.BaseAttrInfoService;
import com.rao.gmall.product.service.BaseAttrValueService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author nzmxfan
 * @create 2021-08-02-18:43
 */
@RestController
@RequestMapping("/admin/product")
public class BaseAttrInfoController {

    @Resource
    BaseAttrInfoService baseAttrInfoService;

    @Resource
    BaseAttrValueService baseAttrValueService;

    /**
     * 查询平台属性值
     *
     * @param category1Id 一级分类的 id
     * @param category2Id 二级分类的 id
     * @param category3Id 三级分类的 id
     * @return 平台属性值的集合
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(@PathVariable("category1Id") Long category1Id,
                                                   @PathVariable("category2Id") Long category2Id,
                                                   @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> baseAttrList = baseAttrInfoService.getBaseAttrList(category1Id, category2Id, category3Id);
        if (baseAttrList.size() > 0) {
            return Result.ok(baseAttrList);
        } else {
            return Result.build(null, ResultCodeEnum.SECKILL_FAIL);
        }
    }

    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId) {

        List<BaseAttrValue> baseAttrList = baseAttrValueService.getAttrValueList(attrId);
        if (baseAttrList.size() > 0) {
            return Result.ok(baseAttrList);
        } else {
            return Result.build(null, ResultCodeEnum.SECKILL_FAIL);
        }
    }

    @PostMapping("/saveAttrInfo")
    public Result saveOrUpdateAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {

        baseAttrInfoService.saveOrUpdateAttrInfo(baseAttrInfo);
        return Result.ok();

    }
}
